package com.hj.chartdemo.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.mikephil.charting.data.Entry
import com.hj.chartdemo.R
import com.hj.chartdemo.bean.chart.ChartTabData
import com.hj.chartdemo.bean.chart.ChartTableData
import com.hj.chartdemo.chart.BarChartManagger
import com.hj.chartdemo.chart.LineChartManagger
import com.hj.chartdemo.databinding.ActivityChartBinding
import com.hj.chartdemo.databinding.ActivityTableBinding
import com.hj.chartdemo.table.ContentAdapter
import com.hj.chartdemo.table.TopTabAdpater
import com.hj.chartdemo.utils.BaseActivity
import com.hj.chartdemo.viewmodel.ChartActivityViewModel
import com.hj.chartdemo.viewmodel.TableActivityViewModel
import java.util.ArrayList


class TableActivity : BaseActivity<ActivityTableBinding>(){

    var topTabAdpater: TopTabAdpater? = null
    var contentAdapter: ContentAdapter? = null

    override fun initContentView(): Int {
        return R.layout.activity_table
    }

    override fun initViewModel(): ViewModel? {
        return ViewModelProvider(this).get(TableActivityViewModel::class.java)
    }

    override fun initView() {
        //处理顶部标题部分
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        binding.rvTabRight.setLayoutManager(linearLayoutManager)
        topTabAdpater = TopTabAdpater(this)
        binding.rvTabRight.setAdapter(topTabAdpater)

        //处理内容部分
        binding.recyclerContent.setLayoutManager(LinearLayoutManager(this))
        binding.recyclerContent.setHasFixedSize(true)
        contentAdapter = ContentAdapter(this, binding.rvTabRight)
        binding.recyclerContent.adapter = contentAdapter

        binding.swipeRefresh.isEnabled = false
    }

    override fun initData() {
        binding.viewModel?.chartData?.observe(this, Observer { it ->
            loadChartData(it)
        })
    }

    private fun loadChartData(chartData:ChartTableData) {
        topTabAdpater?.setDatas(chartData.field)
        contentAdapter?.setDatas(chartData.datas)
    }
}
